package oboe.controllers.jml.helpers;

import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;

import oboe.controllers.jml.events.JMLEvent;
import oboe.controllers.jml.helpers.scrollBar.Bar;
import oboe.controllers.jml.helpers.scrollBar.HBar;
import oboe.controllers.jml.helpers.scrollBar.VBar;

class JMLClipper extends Sprite
{
public var showHBar:Bool = false;
public var showVBar:Bool = false;

public function new()
{
	super();
	this.addEventListener( JMLEvent.JMLCOMPLETE, onComplete);
	Object( this ).addCallback( Event.ADDED_TO_STAGE, onAdd ).once = true;
}

private function onAdd():Void
{
	if( showHBar ) this.parent.addChild( hbar = new HBar( this ) );
	if( showVBar ) this.parent.addChild( vbar = new VBar( this ) );
}

private function onComplete( e:JMLEvent ):Void
{
	if( e.source.width > 0 && e.source.height > 0 )
	{
	var clip:Shape = new Shape();
	clip.graphics.beginFill(0xFFFFFF, 1);
	clip.graphics.drawRect(0, 0, e.source.width, e.source.height );
	this.addChild( clip );
	clip.visible = false;
	this.mask = clip;
	this.scaleX = this.scaleY = 1;
	}
	// this.cacheAsBitmap = true;
}

private var vbar:Bar;
private var hbar:Bar;

public function setScrollbarVisible( state:Bool ):Void
{
	if( !state )
	{
	if( vbar && vbar.parent == this ) this.parent.removeChild( vbar );
	if( hbar && vbar.parent == this ) this.parent.removeChild( hbar );
	}
	else
	{
	if( vbar ) this.parent.addChild( vbar );
	if( hbar ) this.parent.addChild( hbar );
	}
}
}